package it.trick.admin.service;

import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import it.trick.admin.App;
import it.trick.admin.Constants;
import it.trick.admin.model.PreModel;
import java.net.URL;

public class PreService {

    /**
     * PREリストの取得
     * @param page
     * @return
     * @throws Exception
     */
    public static List<PreModel> getList(String page) throws Exception {

        String json = Http.get(App.getInstance().getSite() + Constants.GET_PRE_LIST_PATH + page);
        Type collectionType = new TypeToken<Collection<PreModel>>() {
        }.getType();

        Gson gson = new Gson();

        List<PreModel> modelList = gson.fromJson(json, collectionType);

        return modelList;
    }

    /**
     * 追加
     * @param model
     * @return
     * @throws Exception
     */
    public static PreModel addPre(PreModel model) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.ADD_PRE_PATH);
        Type responseModelType = new TypeToken<PreModel>() {}.getType();

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("page=");
        psb.append(model.getPrarentKeyString());
        psb.append("&");
        psb.append("name=");
        psb.append(model.getName());
        psb.append("&");
        psb.append("index=");
        psb.append(model.getIndex());
        psb.append("&");
        psb.append("content=");
        psb.append(model.getContent());

        return (PreModel)Http.postGetResponseJson(postURL, psb.toString(), responseModelType);
    }

    /**
     * PREの取得
     * @return
     * @throws Exception
     */
    public static PreModel getPre(String preKeyString) throws Exception {

        String json = Http.get(App.getInstance().getSite() + Constants.GET_PRE_PATH + preKeyString);
        Type type = new TypeToken<PreModel>() {}.getType();

        Gson gson = new Gson();

        PreModel model = gson.fromJson(json, type);

        return model;
    }

    /**
     * 更新
     * @param chapterModel
     * @throws Exception
     */
    public static void updatePre (
            PreModel preModel) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.UPDATE_PRE_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("pre=");
        psb.append(preModel.getKeyValueString());
        psb.append("&");
        psb.append("name=");
        psb.append(preModel.getName());
        psb.append("&");
        psb.append("index=");
        psb.append(preModel.getIndex());
        psb.append("&");
        psb.append("content=");
        psb.append(preModel.getContent());

        Http.postOnle(postURL, psb.toString());
    }

    /**
     * 削除
     * @param KeyString
     */
    public static void deletePre(String KeyString) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.DELETE_PRE_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("pre=");
        psb.append(KeyString);

        Http.postOnle(postURL, psb.toString());
    }

    /**
     * 公開・非公開
     * @param categoryKeyString
     */
    public static void publicPre(String KeyString) throws Exception {

        URL postURL = new URL(App.getInstance().getSite() + Constants.PUBLIC_PRE_PATH);

        //POSTの内容を書き出す
        StringBuilder psb = new StringBuilder();
        psb.append("pre=");
        psb.append(KeyString);

        Http.postOnle(postURL, psb.toString());
    }
}
